/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample;

import static org.junit.Assert.assertEquals;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.global.resource.RawFileDescriptor;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * GifImageViewTest
 */
public class GifImageViewTest {
    private static final int LOOP_COUNT_0 = 0;
    private static final int LOOP_COUNT_5 = 5;
    private static final int FRAME_COUNT = 4;  // Number of frames in the given gif file

    ComponentContainer rootLayout;
    GifDrawable gifDrawable ;
    GifImageView gifView;
    private Context mContext;

    /**
     * setUp
     */
    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_ability_gif, null, false);
        gifView = (GifImageView) rootLayout.findComponentById(ResourceTable.Id_GifRawFD);

        RawFileDescriptor rawFileDescriptor = null;
        try {
            rawFileDescriptor = mContext.getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor();
            gifDrawable = new GifDrawable(rawFileDescriptor); // using assetfiledescriptor
        } catch (IOException e) {
            e.printStackTrace();
        }

        gifView.setImageElement(gifDrawable);
        gifView.setBackgroundElement(gifDrawable);
    }

    /**
     * testLoopCountNotSet
     */
    @Test
    public void testLoopCountNotSet() {
        GifDrawable element = (GifDrawable) gifView.getImageElement();
        assertEquals(element.getLoopCount(), LOOP_COUNT_0);
    }

    /**
     * testLoopCountSet
     */
    @Test
    public void testLoopCountSet() {
        GifDrawable element = (GifDrawable) gifView.getImageElement();
        element.setLoopCount(LOOP_COUNT_5);
        assertEquals(element.getLoopCount(), LOOP_COUNT_5);
    }

    /**
     * testLoopCountNotSetForBackground
     */
    @Test
    public void testLoopCountNotSetForBackground() {
        GifDrawable element = (GifDrawable) gifView.getBackgroundElement();
        assertEquals(element.getLoopCount(), LOOP_COUNT_0);
    }

    /**
     * testLoopCountSetForBackground
     */
    @Test
    public void testLoopCountSetForBackground() {
        GifDrawable element = (GifDrawable) gifView.getBackgroundElement();
        element.setLoopCount(LOOP_COUNT_5);
        assertEquals(element.getLoopCount(), LOOP_COUNT_5);
    }

    /**
     * testNumberOfFrames
     */
    @Test
    public void testNumberOfFrames() {
        GifDrawable element = (GifDrawable) gifView.getImageElement();
        assertEquals(element.getNumberOfFrames(), FRAME_COUNT);
    }
}